﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class The_hien_TH_GIO_HANG : System.Web.UI.UserControl
{
    void lien_ket_du_lieu()
    {
        gview_giohang.DataSource = (ArrayList)Session["Gio_hang"];
        gview_giohang.DataBind();
        //cập nhật tổng tiền và số lượng
        lbl_tongtien.Text = tongtien().ToString("#,##0 VND");
        lbl_tonghang.Text = ((ArrayList)Session["Gio_hang"]).Count.ToString();
    }
    double tongtien()
    {
        ArrayList gio_hang = ((ArrayList)Session["Gio_hang"]);
        double tong = 0.0;
        int sosach = 0;
        for (int i = 0; i <= gio_hang.Count - 1; i++)
        {
            tong += ((GIO_HANG)gio_hang[i]).Thanh_tien;
            sosach += ((GIO_HANG)gio_hang[i]).So_luong;
        }
        Session["So_tien"] = tong;//:)
        Session["So_sach"] = sosach;
        return tong;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string jsScript = "<script language='JavaScript' src='../The_hien/checkbox.js'></script>";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", jsScript);
        if (!IsPostBack)
        {
            lien_ket_du_lieu();
            PanelTTDonHang.Visible = false;

        }
        Button_Luu_Don_Hang();
        CheckGioHang();
    }

    private void RegisterClientScriptBlock(string p, string jsScript)
    {
        throw new NotImplementedException();
    }

    protected void gview_giohang_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gview_giohang.EditIndex = -1;
        lien_ket_du_lieu();
    }
    protected void gview_giohang_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gview_giohang.EditIndex = e.NewEditIndex;
        lien_ket_du_lieu();
    }
    protected void gview_giohang_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtsl=(TextBox)gview_giohang.Rows[e.RowIndex].Cells[1].Controls[0];
        int so_luong = 1;
        try
        {
            so_luong = int.Parse(txtsl.Text);
        }
        catch
        { }
        if (so_luong > 3)
            so_luong = 3;
        else if (so_luong < 1)
            so_luong = 1;
        ArrayList gio_hang = (ArrayList)Session["Gio_hang"];
        ((GIO_HANG)gio_hang[e.RowIndex]).So_luong = so_luong;
        Session["Gio_hang"] = gio_hang;
        //trả gridview về trạng thái ban đầu
        gview_giohang.EditIndex = -1;
        lien_ket_du_lieu();
    }
    protected void gview_giohang_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btn_xoa_Click(object sender, EventArgs e)
    {

    }
    protected void gview_giohang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ArrayList gio_hang = (ArrayList)Session["Gio_hang"];
        //int masach = ((GIO_HANG)gio_hang[e.RowIndex]).Ms;
        gio_hang.RemoveAt(e.RowIndex);
        Session["Gio_hang"] = gio_hang;
        //trả gridview về trạng thái ban đầu
        gview_giohang.EditIndex = -1;
        lien_ket_du_lieu();
        Button_Luu_Don_Hang();
        CheckGioHang();
    }

    protected void gview_giohang_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "xoaall")
        {
            ArrayList gio_hang = (ArrayList)Session["Gio_hang"];
            for (int i = gview_giohang.Rows.Count - 1; i >= 0; i--)
                if (((CheckBox)(gview_giohang.Rows[i].FindControl("chk_item"))).Checked)
                    gio_hang.RemoveAt(i);
            Session["Gio_hang"] = gio_hang;
            lien_ket_du_lieu();
            Button_Luu_Don_Hang();
        }
    }


    protected void gview_giohang_DataBound(object sender, EventArgs e)
    {

    }


    private void Button_Luu_Don_Hang()//phải đăng nhập và có sp mới lưu
    {
        //if (Session["Dang_nhap"] != null)
            if (int.Parse(Session["So_sach"].ToString()) != 0)
            {
                btn_dat_hang.Enabled = true;
                return;
            }

        btn_dat_hang.Enabled = false;
    }



    protected void btn_dat_hang_Click(object sender, EventArgs e)
    {

        if (Session["Khach_hang"] == null)
        {
            Response.Write("<script>alert('Bạn phải đăng nhập!')</script>");
        }
        else
        {
            PanelTTDonHang.Visible = true;
            btn_dat_hang.Enabled = false;
            gview_giohang.Enabled = false;
        }

    }

    private void CheckGioHang()
    {
        ArrayList gio_hang = ((ArrayList)Session["Gio_hang"]);
        if (gio_hang.Count > 0)
        {
            PanelCoGH.Visible = true;
            PanelKoGH.Visible = false;
        }
        else
        {
            PanelCoGH.Visible = false;
            PanelKoGH.Visible = true;
        }
    }
}